use tokio::sync::mpsc::UnboundedSender;

use crate::data_channels::message::Message;

use super::ice::prepare_follower_channel;

pub fn get_sender(peer_id: &String) -> anyhow::Result<UnboundedSender<Message>> {
    //
    let peer_connection = crate::transport::ice::get_peer_connection(peer_id)?;

    //获取sender
    let sender = {
        peer_connection
            .lock()
            .map_err(|err| anyhow::anyhow!("{}", err))?
            .sender
            .clone()
    };

    sender.map_or(Err(anyhow::anyhow!("没有获取到!")), |v| Ok(v))
}

pub async fn prepare_follower(remote_peer_id: &String) -> anyhow::Result<()> {
    let _ = prepare_follower_channel(remote_peer_id).await;

    Ok(())
}

pub fn exist_connection(peer_id: &String) -> anyhow::Result<bool> {
    //
    crate::transport::ice::peer_connection_exist(peer_id)
}

pub fn is_setuped(peer_id: &String) -> anyhow::Result<bool> {
    crate::transport::ice::is_setuped(peer_id)
}

pub fn remove_connection(peer_id: &String) -> anyhow::Result<()> {
    crate::transport::ice::remove_peer_connection(peer_id)
}
